package com.demo.window.insets

import android.os.Build
import android.util.Log
import android.view.Surface
import android.view.View
import android.widget.Toast
import androidx.core.view.WindowInsetsCompat
import com.demo.window.insets.databinding.ActivityMain2Binding

class Main2Activity : BaseBindingActivity2<ActivityMain2Binding>(ActivityMain2Binding::inflate),
    View.OnClickListener {

    //block = { inflater, parent, attachToParent ->
    //        ActivityMain2Binding.inflate(inflater, parent, attachToParent)
    //    }

    override fun initWindowInsets(v: View, insets: WindowInsetsCompat) {
        super.initWindowInsets(v, insets)
//        val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//        val top = systemBars.top
//        val bottom = systemBars.bottom
//        v.setPadding(0, 0, 0, bottom)
//        Log.d(
//            "tag_main2",
//            "RootWindowInsets: statusBarHeight: $top，navBarHeight: $bottom"
//        )
//        val lp = mViewBinding.btn1.layoutParams
//        if (lp is ConstraintLayout.LayoutParams) {
//            lp.topMargin = top
//            mViewBinding.btn1.layoutParams = lp
//        }

        val displayCutout = insets.getInsets(WindowInsetsCompat.Type.displayCutout())
        val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())

        val rotation = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            this@Main2Activity.display?.rotation ?: 0
        } else {
            windowManager?.defaultDisplay?.rotation ?: 0
        }
        Log.d("tag_main2", "RootWindowInsets: rotation: $rotation")
        when (rotation) {
            Surface.ROTATION_0 -> {
                val top = displayCutout.top
                val bottom = systemBars.bottom
                v.setPadding(0, top, 0, bottom)
            }

            Surface.ROTATION_90 -> {
                val left = displayCutout.left
                val top = systemBars.top
                val right = systemBars.right
                v.setPadding(left, top, right, 0)
            }

            Surface.ROTATION_180 -> {

            }

            Surface.ROTATION_270 -> {
                val left = systemBars.left
                val top = systemBars.top
                val right = displayCutout.right
                v.setPadding(left, top, right, 0)
            }
        }
    }

    override fun initData() {
        super.initData()
        Log.d("tag_main2", "onCreate")
        mViewBinding.btn1.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn1 -> {
                Toast.makeText(this@Main2Activity, "top", Toast.LENGTH_SHORT).show()
            }
        }
    }
}